/*
 * Mesh.cpp
 *
 *  Created on: 20-dec.-2013
 *      Author: Axel
 */

#include <Mesh.h>

#include <GL/glew.h>
#include "extraMath.h"

Mesh::Mesh(float x, float y, float z) {
	 edge.vertices = new Vector3f[1];
	 index.indices = new GLuint[1];
	 col.color = new GLfloat[1];

	 model = new ModelView(x,y,z);
	 shape = new Shapes();
}

Mesh::~Mesh() {
	delete edge.vertices;
	delete index.indices;
	delete col.color;
	delete shape;
	delete model;
}

void Mesh::move(float x, float y, float z) {
	model->move(x,y,z);
}

Vector4f Mesh::getLocation() {
	return model->getLocation();
}


void Mesh::makeCube(float width) {
	shape->makeCube(width, index, edge);
}

void Mesh::makeCircle(float width) {
	shape->makeCircle(width,index,edge);
}

void Mesh::makeRect(float width, float height) {
	shape->makeRect(width, height, index, edge);
}


void Mesh::drawFigure() {
	GLuint vbo, vbo2;
	glGenBuffers(1, &vbo);
	glGenBuffers(1, &vbo2);

	Vector3f Vertices[edge.len];
	for(int i=0;i<edge.len;i++)
		Vertices[i] = model->toWorld(edge.vertices[i]);

	GLuint Indices[index.len];
	for(int i=0;i<index.len;i++)
		Indices[i] = index.indices[i];

	glEnableVertexAttribArray(0);

	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER,sizeof(Vertices), Vertices, GL_DYNAMIC_DRAW);

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo2);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(Indices), Indices, GL_DYNAMIC_DRAW);

	glDrawElements(GL_TRIANGLES, index.len, GL_UNSIGNED_INT, 0);

	glDisableVertexAttribArray(0);

	glDeleteBuffers(1, &vbo);
	glDeleteBuffers(1, &vbo2);
}



